package com.rzjm.exercise.module.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.SurveyQuestion;
import com.rzjm.exercise.module.domain.dto.QuestionDto;
import com.rzjm.exercise.module.service.SurveyQuestionService;
import com.rzjm.exercise.module.service.SurveyService;
import com.rzjm.exercise.sys.system.user.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 问卷问题列表
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2021-11-5 12:36:23
 */
@Controller
@RequestMapping("/module/survey/question")
public class SurveyQuestionController {
    @Resource
    private SurveyQuestionService surveyQuestionService;

    @Log("查询问卷问题列表页面")
    @GetMapping("/{id}")
    @RequiresPermissions("module:survey:question:survey")
    String question(Model model,@PathVariable("id") Integer id) {
        model.addAttribute("surveyId",id);
        return "module/survey/question/question";
    }

    @Log("查询问卷问题列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:survey:question:survey")
    public DataTableLayUI<SurveyQuestion> list(DataTableLayUI<SurveyQuestion> condition) {
        DataTableLayUI<SurveyQuestion> dt = surveyQuestionService.pageSearchLayUI(condition);
        return dt;
    }

    @Log("新增问卷问题列表页面")
    @GetMapping("/add/{surveyId}")
    @RequiresPermissions("module:survey:question:add")
    String add(Model model,@PathVariable("surveyId") Integer surveyId) {
        SurveyQuestion question = new SurveyQuestion();
        question.setSurveyId(surveyId);
        List<QuestionDto> questionDtoList = new ArrayList<>();
        for (int i = 0;i < 4; i++){
            QuestionDto q = new QuestionDto();
            q.setIndex(i);
            q.setValue("");
            q.setCheck("");
            questionDtoList.add(q);
        }
        question.setSections(questionDtoList);
        question.setSectionsCount(questionDtoList.size());
        model.addAttribute("question", question);
        return "module/survey/question/detail";
    }


    @Log("编辑问卷问题列表页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:survey:question:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        SurveyQuestion question = surveyQuestionService.findById(id);
        if(question!=null){
            if(StrUtil.isNotEmpty(question.getAnswerSection())){
                JSONArray jsonArray = JSONUtil.parseArray(question.getAnswerSection());
                List<QuestionDto> questionDtoList = JSONUtil.toList(jsonArray,QuestionDto.class);
                question.setSections(questionDtoList);
                question.setSectionsCount(questionDtoList.size());
            }else{
                question.setSectionsCount(0);
            }
        }
        model.addAttribute("question", question);
        return "module/survey/question/detail";
    }

    /**
     * 保存
     */
    @Log("新增问卷问题列表")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:survey:question:add")
    public R save(@Valid SurveyQuestion question) {
        if (surveyQuestionService.save(question)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新问卷问题列表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:survey:question:edit")
    public R update(@Valid SurveyQuestion question) {
        surveyQuestionService.updateById(question);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除问卷问题列表")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:survey:question:remove")
    public R remove( Integer id) {
        if (surveyQuestionService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
